package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IDirectSeedDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.subclass.displayBean.directSeed.DirectSeedExtendBean;
import com.wsj.web.vo.homeSite.DirectSeed;

public class DirectSeedDaoImpl extends HibernateDaoSupport implements IDirectSeedDao {

	public void add(DirectSeed directSeed) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().save(directSeed);

	}

	public void delete(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		DirectSeed directSeed=null;
		directSeed=(DirectSeed) this.getHibernateTemplate().get(DirectSeed.class, id);
		this.getHibernateTemplate().delete(directSeed);

	}

	public DirectSeed getDirectSeedById(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		DirectSeed directSeed=null;
		directSeed=(DirectSeed) this.getHibernateTemplate().get(DirectSeed.class, id);
		
		return directSeed;
	}

	public PageResultSet loadDirectSeedByExtendBean(DirectSeedExtendBean directSeedExtendBean)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from DirectSeed as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(directSeedExtendBean.getPage(), directSeedExtendBean.getPageSize());
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //标题
        if(directSeedExtendBean.getTitle()!= null&&!directSeedExtendBean.getTitle().equals("")){
            stringBuffer.append(" and m.title like ?");
            
            list.add("%"+directSeedExtendBean.getTitle()+"%");
        }
          //内容
        if(directSeedExtendBean.getContent()!= null&&!directSeedExtendBean.getContent().equals("")){
        	stringBuffer.append(" and m.content like ?");
        	
        	list.add("%"+directSeedExtendBean.getContent()+"%");
        }
          //关键字
        if(directSeedExtendBean.getKeyword()!= null&&!directSeedExtendBean.getKeyword().equals("")){
        	stringBuffer.append(" and m.keyword like ?");
        	
        	list.add("%"+directSeedExtendBean.getKeyword()+"%");
        }
        //删除
        if(directSeedExtendBean.getShanchu()!= null&&directSeedExtendBean.getShanchu()!=-2){
        	stringBuffer.append(" and m.shanchu = ?");
        	
        	list.add(directSeedExtendBean.getShanchu());
        }
        //对关联直播名表及直播类别字段的查询
        if(directSeedExtendBean.getDirectSeedNameid()!= null&&directSeedExtendBean.getDirectSeedNameid()!=-2){
        	stringBuffer.append(" and m.directSeedClass.directSeedName.id = ?");
        	
        	list.add(directSeedExtendBean.getDirectSeedNameid());
        }
        if(directSeedExtendBean.getDirectSeedClassid()!= null&&directSeedExtendBean.getDirectSeedClassid()!=-2){
        	stringBuffer.append(" and m.directSeedClass.id = ?");
        	
        	list.add(directSeedExtendBean.getDirectSeedClassid());
        }
		
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;

	}

	public List loadDirectSeedList() throws DataAccessException {
		// TODO Auto-generated method stub
		List list=new ArrayList();
		list=this.getHibernateTemplate().find("from DirectSeed where 1=1");
		return list;
	}

	public void update(DirectSeed directSeed) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().update(directSeed);

	}

	public List getTopDirectSeedByHistory(int top) throws DataAccessException {
		// TODO Auto-generated method stub
		
		String sql=" from DirectSeedName as d where 1=1 and d.del=0 and d.qiimage2!=null order by d.inputtime desc";
		
		Query q=this.getSession().createQuery(sql);
		q.setFirstResult(1);
		q.setMaxResults(6);
		return q.list();
	}

	public List getTopDirectSeedByDirectClassName(String lanmuname, Integer top) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeed as d where 1=1 and d.directSeedClass.lanmuname =? and d.islock=0 and d.shanchu=0 and d.isimg=1 order by d.id desc";
		Query q=this.getSession().createQuery(sql);
		q.setParameter(0, lanmuname);
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
	}

	public List getTopByDclassName(String lanmuname, Integer top) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeed as d where 1=1 and d.directSeedClass.lanmuname =? and d.islock=0 and d.shanchu=0 and d.iscommend=1 order by d.id desc";
		Query q=this.getSession().createQuery(sql);
		q.setParameter(0, lanmuname);
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
	}

	public List getDirectSeedByparentId(Integer id, Integer top) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeed as d where 1=1 and d.directSeedClass.id =? and d.islock=0 and d.shanchu=0   order by d.id desc";
		Query q=this.getSession().createQuery(sql);
		q.setParameter(0, id);
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
	}

}













